package com.api.sw.duban.util;

import com.alibaba.fastjson.JSONObject;
import com.api.sw.duban.model.Uf_jcdcrw;
import com.api.sw.duban.model.Uf_jcgzsd;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.MainTableInfo;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.RequestService;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

public class UploadWord {
    private final static LogTool log = new LogTool("/log/duban/",false);
    public String UploadWord(InputStream in, List<Uf_jcdcrw> Uf_jcdcrw, String tzid ) {
        if (in == null) {
            return "文件不能改为空";
        }
        try {
             Uf_jcgzsd uf_jcgzsd =getUf_jcgzsdBytzid(tzid);
             if(uf_jcgzsd.getMaxcel()<=0){
                 return "未设置规则!";
             }
            String wfkey= System.currentTimeMillis() +"";
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            HWPFDocument hwpf = new HWPFDocument(pfs);
            Range range = hwpf.getRange();// 得到文档的读取范围
            TableIterator it = new TableIterator(range);
            // 迭代文档中的表格
            String tableName = "第一个";
            int celllen=uf_jcgzsd.getMaxcel();//列数  最多五列   一定要包含 "牵头单位"
            Uf_jcdcrw uf_jcdcrw=null;
            if (it.hasNext()) {
                Table tb = (Table) it.next();
                System.out.println(tb.numRows());
                // 迭代行，默认从0开始,可以依据需要设置i的值,改变起始行数，也可设置读取到那行，只需修改循环的判断条件即可
                if(tb.numRows()<=1){
                    return "必须大于两行数据";
                }
                int rowkey=1;
                for (int i = 1; i < tb.numRows(); i++) {
                    TableRow tr = tb.getRow(i);

                    if(tr.numCells()<3&&tr.numCells()>5){
                        return "列数不能小于3大于五";
                    }else{
                        if(tr.numCells()!=celllen){
                            return "与规则表不符合!";
                        }
                    }
                    if(i==0){
                        String mess=setGzb(tr,uf_jcgzsd);
                        if(!"".equals(mess)){
                            return mess;
                        }
                    }
                    // 迭代列，默认从0开始
                    JSONObject KEYJSON=new JSONObject();//记录
                    for (int j = 1; j <= tr.numCells(); j++) {
                        TableCell td = tr.getCell(j);// 取得单元格
                        uf_jcdcrw=new Uf_jcdcrw();
                        StringBuilder sb=new StringBuilder();
                        // 取得单元格的内容
                        for (int k = 0; k < td.numParagraphs(); k++) {
                            Paragraph para = td.getParagraph(k);
                            String s = para.text();
                            // 去除后面的特殊符号
                            if (null != s && !"".equals(s)) {
                                s = s.substring(0, s.length() - 1);
                            }
                            s = s.trim();
                            sb.append(s + "\t");
                        }
                        if("".equals(sb.toString())){
                            return "第"+i+"的第"+(j+1)+"列不能为空";
                        }
                        int paraid=Util.getIntValue(KEYJSON.getString((j-1)+""),0) ;// 父级ID
                        String domid=getDomidByid(j)+"";//DOMID
                        String lEVEL=getlEVELByid(j);
                        KEYJSON.put(j+"",domid);//设置父级ID
                        uf_jcdcrw.setDomid(domid);
                        uf_jcdcrw.setLevelids(lEVEL);
                        uf_jcdcrw.setSssj(paraid+"");
                        uf_jcdcrw.setSsdctz(tzid);
                        uf_jcdcrw.setGzrw(sb.toString());
                        uf_jcdcrw.setWfkey(wfkey);
                        if(j==celllen){
                            uf_jcdcrw.setSfsrw(0);
                            String ids=dubanTran.getSwDpartmentnameByName(sb.toString());
                            if("".equals(ids)){
                                return "第"+i+"牵头单位不存在";
                            }else{
                                uf_jcdcrw.setQtdw(ids);
                            }
                        }else{
                            uf_jcdcrw.setSfsrw(1);
                        }
                        Uf_jcdcrw.add(uf_jcdcrw);
                    }
                    rowkey++;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String UploadWord2(InputStream in, List<Uf_jcdcrw> Uf_jcdcrw, String tzid){
        StringBuilder sb=new StringBuilder();
        if (in == null) {
            return "文件不能改为空";
        }
        try (
             XWPFDocument doc = new XWPFDocument(in);) {
            Uf_jcgzsd uf_jcgzsd =getUf_jcgzsdBytzid(tzid);
            if(uf_jcgzsd.getMaxcel()<=0){
                return "未设置规则!";
            }
            String wfkey= System.currentTimeMillis() +"";

            JSONObject datajson=new JSONObject();
            List tables = doc.getTables();
            List rows;
            List cells;
            int celllen=uf_jcgzsd.getMaxcel();//列数  最多五列   一定要包含 "牵头单位"
            Uf_jcdcrw uf_jcdcrw=null;
            for (Object table1 : tables) { // 表格属性
                XWPFTable table=(XWPFTable)table1;
                CTTblPr pr = table.getCTTbl().getTblPr(); // 获取表格对应的行
                rows = table.getRows();
                if(rows.size()<=1){
                    return "必须大于两行数据";
                }



                int rowkey=1;
                for (int rowi=1;rowi<rows.size();rowi++) { // 获取行对应的单元格
                    //for (Object row1 : rows) { // 获取行对应的单元格
                    XWPFTableRow row=(XWPFTableRow)rows.get(rowi);
                    cells = row.getTableCells();
                    System.out.println(cells.size());

                    if(cells.size()<3&&cells.size()>5){
                        return "列数不能小于3大于五";
                    }else{
                        if(cells.size()!=(celllen+1)){
                            return "与规则表不符合!";
                        }
                    }
                    //for (Object cell1 : cells) {
                    JSONObject KEYJSON=null;//记录
                    for (int cel1=0;cel1<(cells.size()-1);cel1++) {
                        KEYJSON=new JSONObject();//记录
                        XWPFTableCell cell=(XWPFTableCell)cells.get(cel1);
                        uf_jcdcrw=new Uf_jcdcrw();
                        CTVMerge CTVMerge=cell.getCTTc().getTcPr().getVMerge();
                        String bs="";
                        if(CTVMerge!=null&&CTVMerge.isSetVal()){
                            bs=CTVMerge.getVal().toString();
                        }
                        String data=setDataJson(datajson,cel1,rowi,cell.getText(),bs);
                        if("".equals(data)){
                            return "第"+rowi+"的第"+(cel1+1)+"列不能为空";
                        }
                        int paraid=Util.getIntValue(KEYJSON.getString((cel1-1)+""),0) ;// 父级ID
                        String domid=rowkey+"";//getDomidByid(cel1)+"";//DOMID
                        String lEVEL=getlEVELByid(cel1);
                        KEYJSON.put(cel1+"",domid);//设置父级ID
                        uf_jcdcrw.setDomid(domid);
                        uf_jcdcrw.setLevelids(lEVEL);
                        uf_jcdcrw.setSssj(paraid+"");
                        uf_jcdcrw.setSsdctz(tzid);
                        uf_jcdcrw.setGzrw(sb.toString());
                        uf_jcdcrw.setWfkey(wfkey);
                        if(cel1==(celllen-1)){
                            uf_jcdcrw.setSfsrw(0);
                            XWPFTableCell qtdwcell=(XWPFTableCell)cells.get(cells.size()-1);
                            String ids=dubanTran.getSwDpartmentnameByName(qtdwcell.getText());
                            if("".equals(ids)){
                                return "第"+rowi+"牵头单位不存在";
                            }else{
                                uf_jcdcrw.setQtdw(ids);
                            }
                        }else{
                            uf_jcdcrw.setSfsrw(1);

                        }
                        Uf_jcdcrw.add(uf_jcdcrw);
                        rowkey++;
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public String UploadWord3(InputStream in, List<Uf_jcdcrw> Uf_jcdcrw, String tzid){
        StringBuilder sb=new StringBuilder();
        if (in == null) {
            return "文件不能改为空";
        }
        try (XWPFDocument doc = new XWPFDocument(in);) {
            Uf_jcgzsd uf_jcgzsd =getUf_jcgzsdBytzid(tzid);
            int maxlength=DubUtil.getMaxLength();
            if(uf_jcgzsd.getMaxcel()<=0){
                return "未设置规则!";
            }
            String wfkey= System.currentTimeMillis() +"";
            setRoot(Uf_jcdcrw,tzid,wfkey);//设置根目录
            JSONObject datajson=new JSONObject();
            List tables = doc.getTables();
            List cellsall;
            JSONObject leveljson=new JSONObject();//保留所有的上级
            int celllen=uf_jcgzsd.getMaxcel();//列数  最多五列   一定要包含 "牵头单位"
            log.writeLog(celllen);
            Uf_jcdcrw uf_jcdcrw=null;
            for (Object table1 : tables) { // 表格属性
                XWPFTable table=(XWPFTable)table1;
                //List<Uf_jcdcrw> List=new ArrayList<Uf_jcdcrw>();
                JSONObject json=new JSONObject();
                JSONObject jsonkey=new JSONObject();//记录上级 通过cell 以及行区间
                int mainid=1;

                long rowkey=System.currentTimeMillis();//1;
                int cellz=uf_jcgzsd.getMaxcel();
                for(int cell=0;cell<cellz;cell++){
                    List  rows = table.getRows();
                    int begin=1;
                    int end=1;
                    boolean istart=false;
                    String data="";
                    for (int rowi=1;rowi<rows.size();rowi++) { // 获取行对应的单元格  从第一行开始

                        XWPFTableRow row=(XWPFTableRow)rows.get(rowi);
                        if(rowi==1){
                            cellsall=row.getTableCells();
                            if(cellsall.size()<3&&cellsall.size()>5){
                                return "列数不能小于3大于五";
                            }else{
                                if(cellsall.size()!=(celllen+1)){
                                    log.writeLog(cellsall.size());
                                    return "与规则表不符合!";
                                }
                            }
                        }

                        XWPFTableCell democell=(XWPFTableCell)row.getTableCells().get(cell);


                        if(democell==null){
                            return "列为空";
                        }
                        CTVMerge CTVMerge=democell.getCTTc().getTcPr().getVMerge();
                        if(CTVMerge!=null&&CTVMerge.isSetVal()){
                            //记录一下 开始 以及结束
                            if(!istart){
                                istart=true;
                                begin=rowi;
                                data=democell.getText();
                                continue;
                            }else{
                                if(rowi==(rows.size()-1)){//是否是最后一行
                                    end=rowi;
                                }else{
                                    //判断下一个是不是 合并单元格

                                    if(checkEnd(( (XWPFTableCell)((XWPFTableRow)rows.get(rowi+1)).getCell(cell)))){
                                        continue;//已经设置了 不需要在设置了
                                    }else{//不是的话 设置id
                                        end=rowi;
                                        istart=false;//结束合并
                                    }

                                }

                            }
                        }else{
                            if(istart){//结束合并 说明之前 有合并
                                //System.out.println(rowi);
                                //需要设置上一个
                                end=rowi;
                                istart=false;//结束合并
                                //begin=rowi;//重置第一行
                            }else{
                                begin=rowi;//重置第一行
                                data=democell.getText();
                                //begin=rowi;//重置第一行
                                end=rowi;
                            }
                            if(rowi==(rows.size()-1)) {//是否是最后一行
                                begin=rowi;//重置第一行
                                data=democell.getText();
                                end=rowi;
                            }

                        }

                        JSONObject demonid= jsonkey.getJSONObject(cell+"");
                        if(demonid==null){
                            demonid=new JSONObject();
                            //System.out.println(begin+","+end);
                            demonid.put(begin+","+end,rowkey);
                            jsonkey.put(cell+"",demonid);
                        }else{
                            demonid.put(begin+","+end,rowkey);
                        }
                        //最后一行结束特殊设置
                        //设置数据

                        if("".equals(data.toString())){
                            return "第"+(rowi+1)+"行第"+(cell+1)+"列内容不能为空";
                        }
                        uf_jcdcrw=new Uf_jcdcrw();
                        uf_jcdcrw.setDomid(rowkey+"");


                        //uf_jcdcrw.setLevelids(getlEVELByid(cell)); 重新计算 level
                        String sssj=getSupid(jsonkey,(cell-1),rowi)+"";
                        //jsonkey.put(rowkey+"",sssj);
                        String level=qchx(getLevel(leveljson,rowkey+"",sssj,""));
                        log.writeLog(level);
                        uf_jcdcrw.setLevelids(level); //重新计算 level

                        leveljson.put(rowkey+"",sssj);
                        System.out.println(leveljson.toJSONString());

                        uf_jcdcrw.setSssj(getSupid(jsonkey,(cell-1),rowi)+"");
                        uf_jcdcrw.setSsdctz(tzid);
                        if(maxlength>0){
                            if(data.toString().length()>maxlength){
                                return "第"+(rowi+1)+"行第"+(cell+1)+"列内容超出文本长度"+maxlength;
                            }
                        }
                        uf_jcdcrw.setGzrw(data.toString());
                        uf_jcdcrw.setWfkey(wfkey);
                        if(cell==(cellz-1)){
                            //设置单位 以及 是任务
                            XWPFTableCell bmcell=(XWPFTableCell)row.getTableCells().get(cell+1);
                            if(bmcell!=null){
                                //String bm=Util.null2String(bmcell.getText()).trim() ;
                                String newbm=dubanTran.getSwDpartmentnameByName(getHbDepString(table,rowi,cell+1,jsonkey.getJSONObject(cell+"")));
                                if("".equals(newbm)){
                                    return "第"+(rowi+1)+"行第"+(cell+1)+"牵头单位不能为空";
                                }else{
                                    uf_jcdcrw.setQtdw(newbm);
                                    uf_jcdcrw.setSfsrw(0);
                                }
                            }else{
                                return "第"+(rowi+1)+"行第"+(cell+1)+"牵头单位不能为空";
                            }
                        }else{
                            uf_jcdcrw.setSfsrw(1);
                        }
                        Uf_jcdcrw.add(uf_jcdcrw);
                        rowkey++;

                    }
                }

            }


        } catch (Exception e) {
            e.printStackTrace();
            log.writeLog(e.getMessage());
            return "文件异常";
        }
        for(Uf_jcdcrw uf_jcdcrw1:Uf_jcdcrw){
            if(uf_jcdcrw1!=null){
                String Levelids=uf_jcdcrw1.getLevelids();
                String newdomid=uf_jcdcrw1.getDomid();
                if(!"".equals(Levelids)&&!"1".equals(newdomid)){
                    Levelids="1-"+Levelids;
                }
                uf_jcdcrw1.setLevelids(Levelids);
            }
        }
        log.writeLog(Uf_jcdcrw.toString());
        return "";
    }

    /*
      获取 level
   */
    public static String getLevel(JSONObject JSON,String domid,String sssj,String src){
        if(JSON==null||JSON.size()<=0){
            return domid;
        }else{
             //if("".equals(sssj)||"0".equals(sssj)){
             if("".equals(sssj)||"1".equals(sssj)){
                return src+"-"+domid;
            }else{
                String value=Util.null2String(JSON.getString(sssj)).trim() ;
                //if("".equals(value)||"0".equals(value)){
                if("".equals(value)||"1".equals(value)){
                    return sssj+src+"-"+domid;
                }else{
                    //src=;
                    return getLevel(JSON,domid,value,("-"+sssj+src));
                }
            }

        }

    }
    public static String qchx(String hx){
        hx=Util.null2String(hx);
        if(hx.startsWith("-")){
            hx=hx.substring(1);
        }
        if(hx.endsWith("-")){
            hx=hx.substring(0,hx.length()-1);
        }
        return hx;
    }

    public static String getHbDepString(XWPFTable table,int currentRow,int cel,JSONObject json){
        String bm="";
        if(table==null||currentRow<1||json==null||json.size()<=0||cel<0){
            return "";
        }else{
            Set<String> key=json.keySet();
            for(String str:key){
                str=Util.null2String(str).trim();
                if(!"".equals(str)&&str.split(",").length==2){
                    int begint=Util.getIntValue(str.split(",")[0],-1);
                    int endint=Util.getIntValue(str.split(",")[1],-1);
                    if(begint>0&&endint>0){
                        if(endint==currentRow){
                            List  rows =table.getRows();
                            for( int i=begint;i<=endint;i++){
                                XWPFTableCell xWPFTableCell=table.getRow(i).getCell(cel);
                                //判断是否重复
                                if((","+bm+",").indexOf(","+xWPFTableCell.getText()+",")<0){
                                    bm=bm+xWPFTableCell.getText()+",";
                                }

                            }
                        }
                    }
                }
            }
        }
        return bm;
    }
    /*
            判断是不是最后一个 true 表示下一个是合并的
         */
    public static boolean checkEnd(XWPFTableCell cell){
        if(cell==null){
            return false;
        }else{
            CTVMerge CTVMerge=cell.getCTTc().getTcPr().getVMerge();
            if(CTVMerge!=null&&CTVMerge.isSetVal()&&!"restart".equals(CTVMerge.getVal().toString())){
                return true;
            }else{
                return false;
            }
        }
    }
    /*
       获取paramid
    */
    public static String getSupid(JSONObject json,int cell,int currentrow){
        //String supid="0";
        String supid="1";
        if(json!=null&&json.size()>0){
            JSONObject val= json.getJSONObject(cell+"");
            if(val!=null&&val.size()>0){
                Set<String> keys= val.keySet();
                for(String key:keys){
                    key=Util.null2String(key).trim();
                    if(!"".equals(key)&&key.split(",").length==2){
                        if(Util.getIntValue(key.split(",",-1)[0])<=currentrow&&currentrow<=Util.getIntValue(key.split(",",-1)[1])){
                            return val.getString(key);
                        }
                    }
                }
            }
        }
        return supid;
    }
    public static String setDataJson(JSONObject datajson,int lencel,int rowlen,String data,String bs){
        String newdata="";
        if(datajson!=null){
            JSONObject valueJson=datajson.getJSONObject(lencel+"");
            if(valueJson==null){
                if("restart".equals(bs)){//开始
                    valueJson=new JSONObject();
                    valueJson.put("begrow",rowlen);
                    valueJson.put("max",rowlen);
                    valueJson.put("data",data);
                    datajson.put(lencel+"",valueJson);
                    newdata=data;
                }else{
                    newdata=data;
                }

            }else{
                //结束或者继续
                if("continue".equals(bs)){
                    int demomax= Util.getIntValue(valueJson.getString("max"),-1);
                    if(demomax==(rowlen-1)){
                        valueJson.put("max",rowlen);
                        newdata=valueJson.getString("data");
                    }
                }else if("".equals(bs)||"restart".equals(bs)){
                    //结束
                    valueJson.put("endrow",rowlen);
                    valueJson.put("data",data);
                    newdata=data;
                }
            }
        }
        return newdata;
    }
    /*
        domid 生成规则
     */
    public String getDomidByid(int i){
        if(i==0){
            return "1";
        }else{
            return ((1+"")+(i));
        }

    }
    /*
        lEVEL 生成规则
     */
    public static String getlEVELByid(int i){
        if(i==0){
            return "1";
        }else{
            String lEVEL="1";
            for(int i1=1;i1<=i;i1++){
                lEVEL=lEVEL+"-1"+(i1);
            }
            return lEVEL;
        }

    }
    /*
        设置  规则表
     */
    public String setGzb(TableRow row, Uf_jcgzsd uf_jcgzsd){
            String mess="格式错误";

            if(row!=null){
                for (int j = 0; j < row.numCells(); j++) {
                    TableCell td = row.getCell(j);// 取得单元格
                    StringBuilder sb=new StringBuilder();
                    for (int k = 0; k < td.numParagraphs(); k++) {
                        Paragraph para = td.getParagraph(k);
                        String s = para.text();
                        // 去除后面的特殊符号
                        if (null != s && !"".equals(s)) {
                            s = s.substring(0, s.length() - 1);
                        }
                        sb.append(s );
                    }
                    if(j==0){
                        uf_jcgzsd.setBt1(sb.toString());
                        uf_jcgzsd.setBtxdzs1(500);
                        uf_jcgzsd.setCheck1(1);
                    }
                    if(j==1){
                        uf_jcgzsd.setBt2(sb.toString());
                        uf_jcgzsd.setBtxdzs2(500);
                        uf_jcgzsd.setCheck2(1);
                    }
                    if(j==2){
                        uf_jcgzsd.setBt3(sb.toString());
                        uf_jcgzsd.setBtxdzs3(500);
                        uf_jcgzsd.setCheck3(1);
                    }
                    if(j==3){
                        uf_jcgzsd.setBt4(sb.toString());
                        uf_jcgzsd.setBtxdzs4(500);
                        uf_jcgzsd.setCheck4(1);
                    }
                    if("".equals(sb)){
                        mess="标题列不能为空!";
                    }
                }
            }
            return mess;
    }
    /*
        批量触发流程
     */
    public String CreateWorflow(User user,List<Uf_jcdcrw> Uf_jcdcrw){
        String wfkey="123";
        List<String> list=new ArrayList<String>();
        if(Uf_jcdcrw==null&&Uf_jcdcrw.size()<=0){

        }else{
            SqlUtil sqlUtil=new SqlUtil();//任务名称单独更新
            Map<String,Object> map=null;
            long min=DubUtil.getMin();
            for(Uf_jcdcrw demollf :Uf_jcdcrw){
                if(demollf==null){
                    continue;
                }

                wfkey=demollf.getWfkey();
                //触发流程开始
                String workflowname="任务批量分发" ;
                //log.error("issubmit4--->"+issubmit);
                RequestInfo ri=new RequestInfo();
                //添加主表数据
                MainTableInfo mti = new MainTableInfo();
                //读取创建人
                ri.setCreatorid(1+"");//表单创建人id(申请人)
                //流程ID
                ri.setWorkflowid(DubUtil.wf);//根据问题类型查找对应的流程ID
                //log.error("issubmit5--->"+issubmit);
                //流程标题  Title+"-"+SubCompanyname+"-"+
                String title=workflowname+"-"+user.getLastname();
                ri.setDescription(title);
                ri.setMainTableInfo(mti);
                Property[] p = new Property[0];
                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("gzrw");
                p[p.length-1].setValue(demollf.getGzrw());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("qtdw");
                p[p.length-1].setValue(demollf.getQtdw());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("zt");
                p[p.length-1].setValue(demollf.getZt()+"");

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("ssdctz");
                p[p.length-1].setValue(demollf.getSsdctz());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("sfsrw");
                p[p.length-1].setValue(demollf.getSfsrw()+"");

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("cj");
                p[p.length-1].setValue(demollf.getCj());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("sssj");
                p[p.length-1].setValue(demollf.getSssj());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("levelids");
                p[p.length-1].setValue(demollf.getLevelids());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("domid");
                p[p.length-1].setValue(demollf.getDomid());

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("cjr");
                p[p.length-1].setValue(user.getUID()+"");

                p = Arrays.copyOf(p, (p.length+1));
                p[p.length-1] = new Property();
                p[p.length-1].setName("wfkey");
                p[p.length-1].setValue(demollf.getWfkey());
                //设置主表数据
                mti.setProperty(p);
                RequestService service = new RequestService();
                //service.deleteRequest()
                int requestid;
                try {
                    requestid =Util.getIntValue(service.createRequest(ri),-99)  ;
                    if(requestid<0){
                        deleteRw(wfkey,min);
                        //deleteWf(list);
                        return "异常"+requestid;
                    }else{
                        list.add(requestid+"");
                        //更新任务
                        map=new HashMap<String,Object>();


                    }
                }catch(Exception e){
                    //deleteWf(list);
                    return "异常"+e.getMessage();
                }

            }
        }
        //deleteWf(list);
    return "";
    }
    /*
        删除一些如的 任务
     */
    public void deleteRw(String wfkey,long min){
        //开启线程删除
        new Thread(new Runnable() {
            @Override
            public void run() {
                //延迟十秒
                try {
                    Thread.sleep(min);
                    RecordSet re=new RecordSet();
                    re.execute("update uf_jcdcrw set zt=4 where wfkey='"+wfkey+"'");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }
    /*
        删除流程
     */
    public void deleteWf(List<String> list){

        if(list!=null&&list.size()>0){
            RequestService service = new RequestService();
            for(String str:list){
                str=Util.null2String(str).trim();

                if(!"".equals(str)){
                    service.deleteRequest(Util.getIntValue(str,-1),1);
                }
            }
        }
    }
    /*
        获取规则列表

     */
    public Uf_jcgzsd getUf_jcgzsdBytzid(String id){
        Uf_jcgzsd Uf_jcgzsd=new Uf_jcgzsd();
        RecordSet re=new RecordSet();
        String sql=" select * from uf_jcgzsd where ssdctz='"+id+"'";
        re.execute(sql);
        while(re.next()){
            int len=0;
            String check1=Util.null2String(re.getString("check1"));
            String check2=Util.null2String(re.getString("check2"));
            String check3=Util.null2String(re.getString("check3"));
            String check4=Util.null2String(re.getString("check4"));
            if("1".equals(check4)&&len==0){
                len=4;
                Uf_jcgzsd.setMaxcel(len);
            }
            if("1".equals(check3)&&len==0){
                len=3;
                Uf_jcgzsd.setMaxcel(len);
            }
            if("1".equals(check2)&&len==0){
                len=2;
                Uf_jcgzsd.setMaxcel(len);
            }
            if("1".equals(check1)&&len==0){
                len=1;
                Uf_jcgzsd.setMaxcel(len);
            }
            for(int i=0;i<len;i++){
                String bt=Util.null2String(re.getString("bt"+(i+1))).trim();
                if("".equals(bt)){
                    bt="事项";
                }

            }
        }
        return Uf_jcgzsd;
    }
    /*
        判断是和否存在根目录
     */
    public void setRoot(List<Uf_jcdcrw> Uf_jcdcrw,String ttid,String wfkey){
        RecordSet re=new RecordSet();
        String sql1="select b.bt,b.lb from uf_jcdcrw a,uf_jcdctz b  where a.ssdctz='"+ttid+"' and domid=1 and a.ssdctz=b.id";
        re.execute(sql1);
        if(!re.next()) {
            RecordSet re1 = new RecordSet();
            String sql = "select b.bt,b.lb from uf_jcdctz b  where b.id="+ttid;
            re1.execute(sql);
            if (re1.next()) {
                String bt = Util.null2String(re1.getString("bt"));
                String lb = Util.null2String(re1.getString("lb"));
                Uf_jcdcrw uf_jcdcrw = new Uf_jcdcrw();
                uf_jcdcrw.setSsdctz(ttid);
                uf_jcdcrw.setLb(lb);
                uf_jcdcrw.setGzrw(bt);
                uf_jcdcrw.setDomid(1 + "");
                uf_jcdcrw.setLevelids(1 + "");
                uf_jcdcrw.setSssj(0 + "");
                uf_jcdcrw.setSfsrw(1);
                uf_jcdcrw.setWfkey(wfkey);
                uf_jcdcrw.setQtdw("");
                uf_jcdcrw.setZt(0);
                Uf_jcdcrw.add(uf_jcdcrw);
            }
        }
    }

}

